package controller;

import java.util.ArrayList;
import model.Checkerboard;

/**
 * @author Robert EA
 * @desc : Controleur de la classe Checkerboard.java
 */
public class cCheckerboard {

    private Checkerboard checkerboard;                                                      /*!!! copied to Move !!!*/
    // Management of cache for possible moves                                               /*!!! copied to Move !!!*/

    private boolean possibleMovesCalculated = false;                                        /*!!! copied to Move !!!*/
    // List of possible moves from position possibleMovesPositionX, possibleMovesPositionY  /*!!! copied to Move !!!*/

    private ArrayList<int[]> possibleMoves = new ArrayList<int[]>();                        /*!!! copied to Move !!!*/
    // i-th item is a piece position to remove when using i-th move from possibleMoves      /*!!! copied to Move !!!*/

    private ArrayList<int[]> possibleMovesDelete = new ArrayList<int[]>();                  /*!!! copied to Move !!!*/

    private int possibleMovesPositionX;                                                     /*!!! copied to Move !!!*/

    private int possibleMovesPositionY;                                                     /*!!! copied to Move !!!*/

    private ArrayList<int[]> piecesToRemove = new ArrayList<int[]>(); // list of pieces position to remove when a round finished    /*!!! copied to Move !!!*/

    /**
     * Create new cotroller for Checkerboard
     */
    public cCheckerboard() {
        checkerboard = new Checkerboard();
        initSquare();
        initCheckerboard();
        checkerboard.setIsSelected(false);
        checkerboard.setPlayer(1);
        checkerboard.setNbPlayablePieces(0);
        checkerboard.setNbPiecesPlayer1(20);
        checkerboard.setNbPiecesPlayer2(20);
    }

    /**
     * converts a piece into a visual representation character
     * @param piece
     * @return a character representing
     */
    public static char pieceToChar(int piece) {
        return (piece == Checkerboard.EMPTY ? ' '
                : piece == Checkerboard.P1_PAWN ? 'o'
                : piece == Checkerboard.P1_QUEEN ? 'D'
                : piece == Checkerboard.P2_PAWN ? '*'
                : piece == Checkerboard.P2_QUEEN ? '@'
                : '#');
    }

    /**
     * Initialize a square
     */
    public void initSquare() {
        int i, j, k = 0;
        /* Create and give an id, position (coord x and y) for each square */
        for (j = 0; j < checkerboard.getSize(); j++) {
            for (i = 0; i < checkerboard.getSize(); i++) {
                // checkerboard.getSquare(i, j) = new Square();
                checkerboard.getSquare(i, j).setIdSquare(k);
                checkerboard.getSquare(i, j).setX(i);
                checkerboard.getSquare(i, j).setY(j);
                k++;
            }
        }

        /* Set coloration for each square */
        int resultat;
        for (i = 0; i < checkerboard.getSize(); i++) {
            for (j = 0; j < checkerboard.getSize(); j++) {
                resultat = checkerboard.getSquare(i, j).getX() + checkerboard.getSquare(i, j).getY();
                if (resultat % 2 == 0) {
                    checkerboard.getSquare(i, j).setColor('L');
                } else {
                    checkerboard.getSquare(i, j).setColor('D');
                }
                //System.out.print(checkerboard.getSquare(i, j).getColor() + " | ");
            }
            //System.out.println();
        }
    }

    /**
     * Set position of pawns
     */
    public void initCheckerboard() {

        int i, j;
        for (j = 0; j < checkerboard.getSize(); j++) {
            for (i = 0; i < checkerboard.getSize(); i++) {

                if (checkerboard.getSquare(i, j).getColor() == 'D' && j >= 0 && j <= 3) /* Position of P1's pawn */ {
                    checkerboard.getSquare(i, j).setIsOccupied(Checkerboard.P1_PAWN);
                } else if (checkerboard.getSquare(i, j).getColor() == 'D' && j >= 6 && j <= 9) /* Position of P2's pawn */ {
                    checkerboard.getSquare(i, j).setIsOccupied(Checkerboard.P2_PAWN);
                } else {
                    checkerboard.getSquare(i, j).setIsOccupied(Checkerboard.EMPTY);
                }

                System.out.print(checkerboard.getSquare(i, j).getIsOccupied() + " | ");
            }
            System.out.println();
        }
    }

    /**
     * Display information of each Square in the checkerboard
     */
    public void displayCheckerboard() {
        int i, j;

        for (j = 0; j < checkerboard.getSize(); j++) {
            for (i = 0; i < checkerboard.getSize(); i++) {
                System.out.print(checkerboard.getSquare(i, j).getX() + ";" + checkerboard.getSquare(i, j).getY() + " | ");
            }
            System.out.println();
        }
    }

    /**
     * Display information of each Square in the checkerboard
     */
    public void showCheckerboard() {
        int i, j;

        for (j = 0; j < checkerboard.getSize(); j++) {
            System.out.print(j + " |");
            for (i = 0; i < checkerboard.getSize(); i++) {
                int input = checkerboard.getSquare(i, j).getIsOccupied();
                char output = pieceToChar(input);

                System.out.print(" " + output);
            }
            System.out.println(" |");
        }
        System.out.println("--------------------------------");
    }

    /**
     * Return Checkerboard that controller is linked to
     * @return Checkerboard from model
     */
    public Checkerboard getCheckerboard() {
        return checkerboard;
    }

    public void clearTabPlayablePieces() {
        for (int i = 0; i < 20; i++) {
            checkerboard.setTabPlayablePieces(-1, i);
        }
        checkerboard.setNbPlayablePieces(0);
    }
}
